package com.desuntech.modules.JcjgModule
{
	import C4D.kbmMWConnection;
	import C4D.kbmMWEvent;
	import C4D.kbmMWQuery;
	import C4D.kbmMWResponse;
	import C4D.kbmMWRow;
	
	import com.desuntech.events.TreeItemEvent;
	import com.desuntech.modules.CommonModule;
	import com.desuntech.modules.JcjgModule.popups.CjsjDetails;
	import com.desuntech.modules.JcjgModule.popups.EmployeeRoleDetails;
	import com.desuntech.modules.JcjgModule.popups.JcbzDetails;
	import com.desuntech.modules.JcjgModule.popups.JccsDetails;
	import com.desuntech.modules.JcjgModule.popups.JcfaDetails;
	import com.desuntech.modules.JcjgModule.popups.JchtDetails;
	import com.desuntech.modules.JcjgModule.popups.JcnlDetails;
	import com.desuntech.modules.JcjgModule.popups.JcryDetails;
	import com.desuntech.modules.JcjgModule.popups.JcxmDetails;
	import com.desuntech.modules.JcjgModule.popups.JzryDetails;
	import com.desuntech.modules.JcjgModule.popups.RoleDetails;
	import com.desuntech.modules.JcjgModule.popups.UserJcjgDetails;
	import com.desuntech.modules.JcjgModule.popups.UserZjzDetails;
	import com.desuntech.modules.JcjgModule.views.CjsjContent;
	import com.desuntech.modules.JcjgModule.views.CxtjContent;
	import com.desuntech.modules.JcjgModule.views.GcxxContent;
	import com.desuntech.modules.JcjgModule.views.JcbgContent;
	import com.desuntech.modules.JcjgModule.views.JcfaContent;
	import com.desuntech.modules.JcjgModule.views.JchtContent;
	import com.desuntech.modules.JcjgModule.views.JcryContent;
	import com.desuntech.modules.JcjgModule.views.JzjlContent;
	import com.desuntech.modules.JcjgModule.views.JzryContent;
	import com.desuntech.modules.JcjgModule.views.UserJcjgContent;
	import com.desuntech.modules.JcjgModule.views.UserZjzContent;
	import com.desuntech.modules.JcjgModule.views.XgjlContent;
	import com.desuntech.modules.JcjgModule.views.xtsz.BzkContent;
	import com.desuntech.modules.JcjgModule.views.xtsz.JccsContent;
	import com.desuntech.modules.JcjgModule.views.xtsz.JccsDyBzContent;
	import com.desuntech.modules.JcjgModule.views.xtsz.JcxmContent;
	import com.desuntech.modules.JcjgModule.views.xtsz.JcxmDyBzContent;
	import com.desuntech.modules.JcjgModule.views.xtsz.JsDyQxContent;
	import com.desuntech.modules.events.CRUDEvent;
	import com.desuntech.modules.events.QueryEvent;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.net.SharedObject;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.core.FlexGlobals;
	import mx.events.CloseEvent;
	import mx.events.ListEvent;
	import mx.managers.PopUpManager;
	
	public class JcjgModule extends CommonModule implements JcjgModuleInterface
	{	
		private var _deleteItem:Object;
		private var _index_updateRoleForms:int;
		private var _array_updateRoleForms:Array;
		private var _roleid_updateRoleForms:int;
		private var _forceReloadContent:Boolean;
		private var _insertNextJcxmByBzIndex:int;
		private var _insertNextJcxmByBzItems:Array;
		private var _insertNextJccsByBzIndex:int;
		private var _insertNextJccsByBzItems:Array;
		private var _deleteNextByBzIndex:int;
		private var _deleteNextByBzItems:Vector.<Object>;
		
		public function JcjgModule()
		{
			super();
		}
		
		public function set forceReloadContent(value:Boolean):void
		{
			_forceReloadContent = value;
		}
		
		public function set corpInfoForUser(value:kbmMWRow):void {
			JcjgGlobal.corpInfoForUser = value;
		}
		
		public function set userInfo(value:kbmMWRow):void {
			JcjgGlobal.userInfo = value;
		}
		
		override public function set connection(conn:kbmMWConnection):void {
			super.connection = conn;
			JcjgGlobal.conn = conn;
		}
		
		override protected function get popup():* {
			if(!_popup[_currentView]) {
				// 根据不同视图给出相应的详情窗体
				switch(_currentView) {
					case AppConstants.VIEW_JCRY:
						popup = new JcryDetails();
						(popup as JcryDetails).jcjgAll = (content as JcryContent).cbJcry.jcjgAll;
						break;
					case AppConstants.VIEW_JCJG:
						popup = new UserJcjgDetails();
						break;
					case AppConstants.VIEW_ZJZ:
						popup = new UserZjzDetails();
						break;
					case AppConstants.VIEW_JCXM:
						popup = new JcxmDetails();
						break;
					case AppConstants.VIEW_JCCS:
						popup = new JccsDetails();
						break;
					case AppConstants.VIEW_BZK:
						popup = new JcbzDetails();
						(popup as JcbzDetails).acJcxmL3a = (content as BzkContent).acJcxmL3a;
						(popup as JcbzDetails).acJcxmL3b = (content as BzkContent).acJcxmL3b;
						break;
					case AppConstants.POPUP_JCNL:
						popup = new JcnlDetails();
						break;
					case AppConstants.POPUP_ROLE:
						popup = new RoleDetails();
						break;
					case AppConstants.POPUP_EMPLOYEE_ROLE:
						popup = new EmployeeRoleDetails();
						break;
					case AppConstants.VIEW_JZRY:
						popup = new JzryDetails();
						break;
					case AppConstants.VIEW_JCHT:
						popup = new JchtDetails();
						(popup as JchtDetails).corpInfo = JcjgGlobal.corpInfoForUser;
						break;
					case AppConstants.VIEW_JCFA:
						popup = new JcfaDetails();
						(popup as JcfaDetails).corpInfo = JcjgGlobal.corpInfoForUser;
						var soUser:SharedObject = SharedObject.getLocal( "user" );
						queryAll([{table: AppConstants.JCJG_JCRY, view: AppConstants.POPUP_JCFA, where: 'CorpId = '+soUser.data.corpid}]);
						break;
					case AppConstants.VIEW_CJSJ:
						popup = new CjsjDetails();
						break;
				}
			}
			return _popup[_currentView];
		}
		
		override protected function get KeyField():String {
			var id:String;
			// 根据不同表设置其对应表的自增型主键的字段名
			switch(_currentTable) {
				case AppConstants.JCJG_JCRY:
					id = 'PersonId';
					break;
				case AppConstants.JCJG_USER:
				case AppConstants.JC_JCXM_BZ:
				case AppConstants.JCJG_GC_RPT:
				case AppConstants.JCJG_AUTO:
				case AppConstants.JCJG_ALTER:
					id = 'AutoId';
					break;
				case AppConstants.JC_JCXM:
					id = 'JCXMID';
					break;
				case AppConstants.JC_JCCS:
					id = 'CSID';
					break;
				case AppConstants.JC_JCBZ:
					id = 'BZID';
					break;
				case AppConstants.JCJG_GCXX:
					id = 'ProjectId';
					break;
				case AppConstants.JCJG_JZR_DJ:
					id = 'JZR_DJ_XH';
					break;
				case AppConstants.JCJG_JCHT:
					id = 'HTID';
					break;
				case AppConstants.JCJG_JCFA:
					id = 'FAID';
					break;
			}
			return id;
		}
		
		// 返回视图内容
		public function getUserJcjgContent():UserJcjgContent {
			_currentView = AppConstants.VIEW_JCJG;
			_currentTable = AppConstants.JCJG_USER;
			staticWhereConditions = 'DWLB = 2'; // 由于检测机构和质监站属于同一个table，故显示不同view时要加上检索条件
			if(!query) query = new kbmMWQuery();
			if(!content || _forceReloadContent) {
				content = new UserJcjgContent();
//				content.addEventListener(JcjgConstants.QUERY_JCJG_FOR_JDZ, queryJcjgForJdz);
				content.addEventListener(JcjgConstants.SHOW_JCZZ_FOR_USER, queryJczzForUser);
				content.addEventListener(JcjgConstants.SEARCH, search);
				addContentCommonELs(content);
//				queryCount();
				queryAll([{table: AppConstants.JCJG_USER, view: AppConstants.VIEW_ZJZ, where: 'DWLB = 1'}, {table: AppConstants.JCJG_USER, view: AppConstants.VIEW_JCJG, where: 'DWLB = 2'}]);
			}
			return content;
		}
		
		public function getUserZjzContent():UserZjzContent
		{
			_currentView = AppConstants.VIEW_ZJZ;
			_currentTable = AppConstants.JCJG_USER;
			staticWhereConditions = 'DWLB = 1';
			if(!query) query = new kbmMWQuery();
			if(!content || _forceReloadContent) {
				content = new UserZjzContent();
				content.addEventListener(JcjgConstants.SEARCH, search);
				addContentCommonELs(content);
			}
			return content;
		}
		
		
		public function getJcryContent(e:QueryEvent = null, justRefreshData:Boolean = false):JcryContent {
			_currentView = AppConstants.VIEW_JCRY;
			_currentTable = AppConstants.JCJG_JCRY;
			staticWhereConditions = 'RYLB != 3';
			if(!query) query = new kbmMWQuery();
			if((!content || _forceReloadContent) && !justRefreshData) {
				content = new JcryContent();
				content.addEventListener(JcjgConstants.QUERY_JCJG_FOR_JDZ, queryJcjgForJdz);
				content.addEventListener(JcjgConstants.SEARCH, search);
				content.addEventListener(JcjgConstants.SHOW_ROLE_FOR_JCRY, queryRoleForEmployee);
				addContentCommonELs(content);
			}
			whereConditions = staticWhereConditions;
			if(e && e.conditions) whereConditions += ' AND ' + e.conditions;
//			queryCount();
			queryAll([{table: AppConstants.JCJG_USER, view: AppConstants.VIEW_ZJZ, where: 'DWLB = 1'}]);
			return content;
		}
		
		public function getJcxmDyBzContent():JcxmDyBzContent
		{
			_currentView = AppConstants.VIEW_JCXMDYBZ;
			if(!query) query = new kbmMWQuery();
			if(!content || _forceReloadContent) {
				content = new JcxmDyBzContent();
				content.addEventListener(ListEvent.ITEM_CLICK, queryBzForXm);
				content.addEventListener(JcjgConstants.QUERY_JCXM_L3_FOR_CATEGORY, queryJcxmL3ForCategory);
				content.addEventListener(JcjgConstants.SEARCH, searchBzForJcxmL3);
				content.addEventListener(CRUDEvent.INSERT, beginInsertJcxmByBz);
				content.addEventListener(CRUDEvent.DELETE, beginDeleteByBz);
				queryAll([{table: AppConstants.JC_JCXM, view: AppConstants.JC_JCXM_0, where: 'CategoryId = 0'}, {table: AppConstants.JC_JCXM, view: AppConstants.JC_JCXM_1, where: 'CategoryId > 0'}, 
					{table: AppConstants.JC_JCXM_L3, view: AppConstants.JC_JCXM_L3_0, where: 'CategoryId = 0'}, {table: AppConstants.JC_JCXM_L3, view: AppConstants.JC_JCXM_L3_1, where: 'CategoryId = 1'}]);
			}
			return content;
		}
		
		public function getJccsDyBzContent():JccsDyBzContent
		{
			_currentView = AppConstants.VIEW_JCCSDYBZ;
			if(!query) query = new kbmMWQuery();
			if(!content || _forceReloadContent) {
				content = new JccsDyBzContent();
				content.addEventListener(ListEvent.ITEM_CLICK, queryBzForCs);
				content.addEventListener(JcjgConstants.QUERY_JCXM_L3_FOR_CATEGORY, queryJcxmL3ForCategory);
				content.addEventListener(JcjgConstants.SEARCH, searchBzForJcxmL3);
				content.addEventListener(CRUDEvent.INSERT, beginInsertJccsByBz);
				content.addEventListener(CRUDEvent.DELETE, beginDeleteByBz);
				queryAll([{table: AppConstants.JC_JCXM, view: AppConstants.JC_JCXM_0, where: 'CategoryId = 0'}, {table: AppConstants.JC_JCXM, view: AppConstants.JC_JCXM_1, where: 'CategoryId > 0'},
					{table: AppConstants.JC_JCCS, view: AppConstants.VIEW_JCCS, where: ''},
					{table: AppConstants.JC_JCXM_L3, view: AppConstants.JC_JCXM_L3_0, where: 'CategoryId = 0'}, {table: AppConstants.JC_JCXM_L3, view: AppConstants.JC_JCXM_L3_1, where: 'CategoryId = 1'}]);
			}
			return content;
		}
		
		public function getJccsContent():JccsContent
		{
			_currentView = AppConstants.VIEW_JCCS;
			if(!query) query = new kbmMWQuery();
			if(!content || _forceReloadContent) {
				content = new JccsContent();
				content.addEventListener(ListEvent.ITEM_CLICK, queryCsForXm);
				addContentCommonELs(content.mg);
				queryAll([{table: AppConstants.JC_JCXM, view: AppConstants.JC_JCXM_0, where: 'CategoryId = 0'}, {table: AppConstants.JC_JCXM, view: AppConstants.JC_JCXM_1, where: 'CategoryId > 0'}]);
			}
			return content;
		}
		
		public function getJcxmContent():JcxmContent
		{
			_currentView = AppConstants.VIEW_JCXM;
			if(!query) query = new kbmMWQuery();
			if(!content || _forceReloadContent) {
				content = new JcxmContent();
				content.addEventListener(CRUDEvent.INSERT, beginInsertJcxm);
				content.addEventListener(CRUDEvent.EDIT, beginEditJcxm);
				content.addEventListener(CRUDEvent.DELETE, beginDeleteJcxm);
				queryAll([{table: AppConstants.JC_JCXM, view: AppConstants.JC_JCXM_0, where: 'CategoryId = 0'}, {table: AppConstants.JC_JCXM, view: AppConstants.JC_JCXM_1, where: 'CategoryId > 0'}]);
			}
			return content;
		}
		
		public function getBzkContent():BzkContent
		{
			_currentView = AppConstants.VIEW_BZK;
			if(!query) query = new kbmMWQuery();
			if(!content || _forceReloadContent) {
				content = new BzkContent();
				content.addEventListener(ListEvent.ITEM_CLICK, queryBzForJcxmL3);
				content.addEventListener(JcjgConstants.SEARCH, searchBzForJcxmL3_Bzk);
				addContentCommonELs(content.mg);
				queryAll([{table: AppConstants.JC_JCXM_L3, view: AppConstants.JC_JCXM_L3_0, where: 'CategoryId = 0'}, {table: AppConstants.JC_JCXM_L3, view: AppConstants.JC_JCXM_L3_1, where: 'CategoryId > 0'}]);
			}
			return content;
		}
		
		public function getJsdyqxContent():JsDyQxContent
		{
			_currentView = AppConstants.VIEW_JSDYQX;
			if(!query) query = new kbmMWQuery();
			if(!content || _forceReloadContent) {
				content = new JsDyQxContent();
				content.addEventListener(JcjgConstants.QUERY_FORMS_BY_ROLE, queryFormsByRole);
				content.addEventListener(JcjgConstants.UPDATE_ROLEFORMS, updateRoleForms);
				content.addEventListener(JcjgConstants.SHOW_ADD_ROLE_POPUP, showAddRolePopup);
				queryAll([{table:AppConstants.SYSAUTH_SYSFORMS, view: AppConstants.SYSAUTH_SYSFORMS}, 
					{table:AppConstants.SYSAUTH_SYSROLES, view:AppConstants.SYSAUTH_SYSROLES}]);
			}
			return content;
		}
		
		public function getCjsjContent(e:QueryEvent = null, justRefreshData:Boolean = false):CjsjContent
		{
			_currentView = AppConstants.VIEW_CJSJ;
			_currentTable = AppConstants.JCJG_AUTO;
			if(!query) query = new kbmMWQuery();
			if((!content || _forceReloadContent) && !justRefreshData) {
				content = new CjsjContent();
				content.addEventListener(JcjgConstants.QUERY_JCJG_FOR_JDZ, queryJcjgForJdz);
				content.addEventListener(JcjgConstants.SEARCH, search);
				addContentCommonELs(content);
			}
			if(e && e.conditions) {
				whereConditions = e.conditions;
			}
//			queryCount();
			queryAll([{table: AppConstants.JCJG_AUTO, view: AppConstants.JCJG_GC_RPT_XMDM, distinct: 'XMDM'}, {table: AppConstants.JCJG_USER, view: AppConstants.VIEW_ZJZ, where: 'DWLB = 1'}]);
			return content;
		}
		
		public function getCxtjContent():CxtjContent
		{
			// todo
			_currentView = AppConstants.VIEW_CXTJ;
//			_currentTable = AppConstants.;
			if(!query) query = new kbmMWQuery();
			if(!content || _forceReloadContent) {
				content = new CxtjContent();
				content.addEventListener(JcjgConstants.QUERY_JCJG_FOR_JDZ, queryJcjgForJdz);
				content.addEventListener(JcjgConstants.SEARCH, search);
			}
//			queryCount();
			queryAll([{table: AppConstants.JCJG_USER, view: AppConstants.VIEW_ZJZ, where: 'DWLB = 1'}]);
			return content;
		}
		
		public function getGcxxContent():GcxxContent
		{
			_currentView = AppConstants.VIEW_GCXX;
			_currentTable = AppConstants.JCJG_GCXX;
			if(!query) query = new kbmMWQuery();
			if(!content || _forceReloadContent) {
				content = new GcxxContent();
				content.addEventListener(JcjgConstants.QUERY_JCJG_FOR_JDZ, queryJcjgForJdz);
				content.addEventListener(JcjgConstants.SEARCH, search);
				addContentCommonELs(content, false);
			}
//			queryCount();
			queryAll([{table: AppConstants.JCJG_USER, view: AppConstants.VIEW_ZJZ, where: 'DWLB = 1'}]);
			return content;
		}
		
		public function getJcbgContent():JcbgContent
		{
			_currentView = AppConstants.VIEW_JCBG;
			_currentTable = AppConstants.JCJG_GC_RPT;
			if(!query) query = new kbmMWQuery();
			if(!content || _forceReloadContent) {
				content = new JcbgContent();
				content.addEventListener(JcjgConstants.QUERY_JCJG_FOR_JDZ, queryJcjgForJdz);
				content.addEventListener(JcjgConstants.SEARCH, search);
				addContentCommonELs(content, false);
			}
//			queryCount();
			queryAll([{table: AppConstants.JCJG_GC_RPT, view: AppConstants.JCJG_GC_RPT_XMDM, distinct: 'XMDM'}, {table: AppConstants.JCJG_USER, view: AppConstants.VIEW_ZJZ, where: 'DWLB = 1'}]);
			return content;
		}
		
		public function getJcfaContent():JcfaContent
		{
			_currentView = AppConstants.VIEW_JCFA;
			_currentTable = AppConstants.JCJG_JCFA;
			if(!query) query = new kbmMWQuery();
			if(!content || _forceReloadContent) {
				content = new JcfaContent();
				content.addEventListener(JcjgConstants.QUERY_JCJG_FOR_JDZ, queryJcjgForJdz);
				content.addEventListener(JcjgConstants.SEARCH, search);
				addContentCommonELs(content);
			}
//			queryCount();
			queryAll([{table: AppConstants.JCJG_USER, view: AppConstants.VIEW_ZJZ, where: 'DWLB = 1'}]);
			return content;
		}
		
		public function getJchtContent():JchtContent
		{
			_currentView = AppConstants.VIEW_JCHT;
			_currentTable = AppConstants.JCJG_JCHT;
			if(!query) query = new kbmMWQuery();
			if(!content || _forceReloadContent) {
				content = new JchtContent();
				content.addEventListener(JcjgConstants.QUERY_JCJG_FOR_JDZ, queryJcjgForJdz);
				content.addEventListener(JcjgConstants.SEARCH, search);
				addContentCommonELs(content);
			}
//			queryCount();
			queryAll([{table: AppConstants.JCJG_USER, view: AppConstants.VIEW_ZJZ, where: 'DWLB = 1'}]);
			return content;
		}
		
		public function getJzjlContent():JzjlContent
		{
			_currentView = AppConstants.VIEW_JZJL;
			_currentTable = AppConstants.JCJG_JZR_DJ;
			if(!query) query = new kbmMWQuery();
			if(!content || _forceReloadContent) {
				content = new JzjlContent();
				content.addEventListener(JcjgConstants.QUERY_JCJG_FOR_JDZ, queryJcjgForJdz);
				content.addEventListener(JcjgConstants.SEARCH, search);
				addContentCommonELs(content, false);
			}
//			queryCount();
			queryAll([{table: AppConstants.JCJG_USER, view: AppConstants.VIEW_ZJZ, where: 'DWLB = 1'}]);
			return content;
		}
		
		public function getJzryContent():JzryContent
		{
			_currentView = AppConstants.VIEW_JZRY;
			_currentTable = AppConstants.JCJG_JCRY;
			staticWhereConditions = 'RYLB = 3';
			if(!query) query = new kbmMWQuery();
			if(!content || _forceReloadContent) {
				content = new JzryContent();
				content.addEventListener(JcjgConstants.SEARCH, search);
				addContentCommonELs(content);
			}
			queryCount();
			return content;
		}
		
		public function getXgjlContent():XgjlContent
		{
			_currentView = AppConstants.VIEW_XGJL;
			_currentTable = AppConstants.JCJG_ALTER;
			if(!query) query = new kbmMWQuery();
			if(!content || _forceReloadContent) {
				content = new XgjlContent();
				content.addEventListener(JcjgConstants.QUERY_JCJG_FOR_JDZ, queryJcjgForJdz);
				content.addEventListener(JcjgConstants.SEARCH, search);
				addContentCommonELs(content, false);
			}
//			queryCount();
			queryAll([{table: AppConstants.JCJG_ALTER, view: AppConstants.JCJG_GC_RPT_XMDM, distinct: 'XMDM'}, {table: AppConstants.JCJG_USER, view: AppConstants.VIEW_ZJZ, where: 'DWLB = 1'}]);
			return content;
		}
		
		
		// insert Jcxm
		private function beginInsertJcxm(e:CRUDEvent):void {
			_currentTable = e.table;
			_currentView = e.view;
			popup.action = CRUDEvent.INSERT;
			popup.item = e.data;
			popup.addEventListener(CloseEvent.CLOSE, closeTW);
			popup.addEventListener(CRUDEvent.INSERT, insertJcxm);
			
			PopUpManager.addPopUp(popup, FlexGlobals.topLevelApplication as DisplayObject, true);
			PopUpManager.centerPopUp(popup);
		}
		
		private function insertJcxm(e:CRUDEvent):void {
			var item:XML = e.data as XML;
			var sql:String = 'INSERT INTO '+_currentTable+' (XMDM, XMMC, CategoryId, DisplayOrder) VALUES ('+item.@XMDM+', \''+item.@XMMC+'\', '+item.@JCXMID+', '+item.@DisplayOrder+')';
			var q:kbmMWQuery = new kbmMWQuery();
			q.Connection = _conn;
			q.Query = sql;
			q.Execute(insertJcxmSuccess, onResolveFailed);
		}
		
		private function insertJcxmSuccess(o:Object):void {
			popup.close();
			queryAll([{table: AppConstants.JC_JCXM, view: AppConstants.JC_JCXM_0, where: 'CategoryId = 0'}, {table: AppConstants.JC_JCXM, view: AppConstants.JC_JCXM_1, where: 'CategoryId > 0'}]);
		}
		
		// edit Jcxm
		private function beginEditJcxm(e:CRUDEvent):void {
			_currentTable = e.table;
			_currentView = e.view;
			popup.action = CRUDEvent.EDIT;
			popup.item = e.data;
			popup.addEventListener(CloseEvent.CLOSE, closeTW);
			popup.addEventListener(CRUDEvent.EDIT, updateJcxm);
			
			PopUpManager.addPopUp(popup, FlexGlobals.topLevelApplication as DisplayObject, true);
			PopUpManager.centerPopUp(popup);
		}
		
		private function updateJcxm(e:CRUDEvent):void {
			var item:XML = e.data as XML;
			var sql:String = 'UPDATE '+_currentTable+' SET XMDM = '+item.@XMDM+', XMMC = \''+item.@XMMC+'\', DisplayOrder = '+item.@DisplayOrder+' WHERE JCXMID = '+item.@JCXMID;
			var q:kbmMWQuery = new kbmMWQuery();
			q.Connection = _conn;
			q.Query = sql;
			q.Execute(updateJcxmSuccess, onResolveFailed);
		}
		
		private function updateJcxmSuccess(o:Object):void {
			popup.close();
			queryAll([{table: AppConstants.JC_JCXM, view: AppConstants.JC_JCXM_0, where: 'CategoryId = 0'}, {table: AppConstants.JC_JCXM, view: AppConstants.JC_JCXM_1, where: 'CategoryId > 0'}]);
		}
		
		// delete Jcxm
		private function beginDeleteJcxm(e:CRUDEvent):void {
			_currentTable = e.table;
			_currentView = e.view;
			_deleteItem = e.data;
			Alert.show('确认删除该条数据？','警告',Alert.OK | Alert.CANCEL, content, deleteJcxm);
		}
		
		private function deleteJcxm(e:CloseEvent):void {
			if (e.detail==Alert.OK) {
				var sql:String = 'DELETE FROM '+_currentTable+' WHERE JCXMID = '+_deleteItem.@JCXMID;
				var q:kbmMWQuery = new kbmMWQuery();
				q.Connection = _conn;
				q.Query = sql;
				q.Execute(deleteJcxmSuccess, onResolveFailed);
			}
		}
		
		private function deleteJcxmSuccess(o:Object):void {
			popup.close();
			queryAll([{table: AppConstants.JC_JCXM, view: AppConstants.JC_JCXM_0, where: 'CategoryId = 0'}, {table: AppConstants.JC_JCXM, view: AppConstants.JC_JCXM_1, where: 'CategoryId > 0'}]);
		}
		
		// insert 检测项目对应标准、检测参数对应标准
		// jcxm
		private function beginInsertJcxmByBz(e:CRUDEvent):void {
			_currentView = e.view;
			_currentTable = e.table;
			_insertNextJcxmByBzItems = (e.target as JcxmDyBzContent).bzsTobeAdd;
			if(_insertNextJcxmByBzItems.length) {
				_insertNextJcxmByBzIndex = 0;
				insertNextJcxmByBz();
			}
		}
		
		private function insertNextJcxmByBz():void {
			var sql:String = 'INSERT INTO '+_currentTable+' (JCXMID, BZID) VALUES ('+_insertNextJcxmByBzItems[_insertNextJcxmByBzIndex]['JCXMID']+', '+_insertNextJcxmByBzItems[_insertNextJcxmByBzIndex]['BZID']+')';
			var q:kbmMWQuery = new kbmMWQuery();
			q.Connection = _conn;
			q.Query = sql;
			q.Execute(insertJcxmByBzSuccess, onResolveFailed);
		}
		
		private function insertJcxmByBzSuccess(o:Object):void {
			_insertNextJcxmByBzIndex++;
			if(_insertNextJcxmByBzIndex < _insertNextJcxmByBzItems.length) {
				insertNextJcxmByBz();
			} else {
				_insertNextJcxmByBzItems = null;
				var e:TreeItemEvent = new TreeItemEvent(ListEvent.ITEM_CLICK);
				e.node = (content as JcxmDyBzContent).treeXm.selectedItem as XML;
				queryBzForXm(e);
			}
		}
		
		// jccs
		private function beginInsertJccsByBz(e:CRUDEvent):void {
			_currentView = e.view;
			_currentTable = e.table;
			_insertNextJccsByBzItems = (e.target as JccsDyBzContent).bzsTobeAdd;
			if(_insertNextJccsByBzItems.length) {
				_insertNextJccsByBzIndex = 0;
				insertNextJccsByBz();
			}
		}
		
		private function insertNextJccsByBz():void {
			var sql:String = 'INSERT INTO '+_currentTable+' (JCCSID, BZID) VALUES ('+_insertNextJccsByBzItems[_insertNextJccsByBzIndex]['JCCSID']+', '+_insertNextJccsByBzItems[_insertNextJccsByBzIndex]['BZID']+')';
			var q:kbmMWQuery = new kbmMWQuery();
			q.Connection = _conn;
			q.Query = sql;
			q.Execute(insertJccsByBzSuccess, onResolveFailed);
		}
		
		private function insertJccsByBzSuccess(o:Object):void {
			_insertNextJccsByBzIndex++;
			if(_insertNextJccsByBzIndex < _insertNextJccsByBzItems.length) {
				insertNextJccsByBz();
			} else {
				_insertNextJccsByBzItems = null;
				var e:TreeItemEvent = new TreeItemEvent(ListEvent.ITEM_CLICK);
				e.node = (content as JccsDyBzContent).treeXm.selectedItem as XML;
				queryBzForCs(e);
			}
		}
		
		// delete 检测项目对应标准、检测参数对应标准
		private function beginDeleteByBz(e:CRUDEvent):void {
			_currentTable = e.table;
			_currentView = e.view;
			_deleteNextByBzItems = e.target.bzsTobeDelete;
			if(_deleteNextByBzItems.length) {
				Alert.show('确认删除数据？','警告',Alert.OK | Alert.CANCEL, content, doDeleteDyBz);
			}
		}
		
		private function doDeleteDyBz(e:CloseEvent):void {
			if (e.detail==Alert.OK) {
				_deleteNextByBzIndex = 0;
				deleteNextByBz();
			}
		}
		
		private function deleteNextByBz():void {
			var sql:String = 'DELETE FROM '+_currentTable+' WHERE AutoId = '+_deleteNextByBzItems[_deleteNextByBzIndex]['AutoId'];
			var q:kbmMWQuery = new kbmMWQuery();
			q.Connection = _conn;
			q.Query = sql;
			q.Execute(deleteByBzSuccess, onResolveFailed);
		}
		
		private function deleteByBzSuccess(result:Object):void {
			_deleteNextByBzIndex++;
			if(_deleteNextByBzIndex < _deleteNextByBzItems.length) {
				deleteNextByBz();
			} else {
				_deleteNextByBzItems = null;
				var e:TreeItemEvent = new TreeItemEvent(ListEvent.ITEM_CLICK);
				switch(_currentView) {
					case AppConstants.VIEW_JCXMDYBZ:
						e.node = (content as JcxmDyBzContent).treeXm.selectedItem as XML;
						queryBzForXm(e);
						break;
					case AppConstants.VIEW_JCCSDYBZ:
						e.node = (content as JccsDyBzContent).treeXm.selectedItem as XML;
						queryBzForCs(e);
						break;
				}
			}
		}
		
		override protected function setCBData(ac:ArrayCollection):void {
			switch(_currentView) {
				case AppConstants.VIEW_JCJG:
					switch(_currentQueryAllView) {
						case AppConstants.VIEW_ZJZ:
							(content as UserJcjgContent).cbUserJcjg.acJdz = ac;
							break;
						/*case AppConstants.VIEW_JCJG:
							(content as UserJcjgContent).cbUserJcjg.acJcjg = ac;
							break;*/
					}
					break;
				case AppConstants.VIEW_JCRY:
					switch(_currentQueryAllView) {
						case AppConstants.VIEW_ZJZ:
							(content as JcryContent).cbJcry.acJdz = ac;
							break;
					}
					break;
				case AppConstants.VIEW_JCXMDYBZ:
					switch(_currentQueryAllView) {
						case AppConstants.JC_JCXM_0:
							(content as JcxmDyBzContent).acJcxm0 = ac;
							break;
						case AppConstants.JC_JCXM_1:
							(content as JcxmDyBzContent).acJcxm1 = ac;
							break;
						case AppConstants.JC_JCXM_L3_0:
							(content as JcxmDyBzContent).ddl0.dataProvider = ac;
							(content as JcxmDyBzContent).ddl0.selectedIndex = 0;
							break;
						case AppConstants.JC_JCXM_L3_1:
							(content as JcxmDyBzContent).ddl1.dataProvider = ac;
							(content as JcxmDyBzContent).ddl1.selectedIndex = 0;
							break;
					}
					break;
				case AppConstants.VIEW_JCCSDYBZ:
					switch(_currentQueryAllView) {
						case AppConstants.JC_JCXM_0:
							(content as JccsDyBzContent).acJcxm0 = ac;
							break;
						case AppConstants.JC_JCXM_1:
							(content as JccsDyBzContent).acJcxm1 = ac;
							break;
						case AppConstants.VIEW_JCCS:
							(content as JccsDyBzContent).acJccs = ac;
							break;
						case AppConstants.JC_JCXM_L3_0:
							(content as JccsDyBzContent).ddl0.dataProvider = ac;
							(content as JccsDyBzContent).ddl0.selectedIndex = 0;
							break;
						case AppConstants.JC_JCXM_L3_1:
							(content as JccsDyBzContent).ddl1.dataProvider = ac;
							(content as JccsDyBzContent).ddl1.selectedIndex = 0;
							break;
					}
					break;
				case AppConstants.VIEW_JCXM:
					switch(_currentQueryAllView) {
						case AppConstants.JC_JCXM_0:
							(content as JcxmContent).acJcxm0 = ac;
							break;
						case AppConstants.JC_JCXM_1:
							(content as JcxmContent).acJcxm1 = ac;
							break;
					}
					break;
				case AppConstants.VIEW_JCCS:
					switch(_currentQueryAllView) {
						case AppConstants.JC_JCXM_0:
							(content as JccsContent).acJcxm0 = ac;
							break;
						case AppConstants.JC_JCXM_1:
							(content as JccsContent).acJcxm1 = ac;
							break;
					}
					break;
				case AppConstants.VIEW_BZK:
					switch(_currentQueryAllView) {
						case AppConstants.JC_JCXM_L3_0:
							(content as BzkContent).acJcxmL3a = ac;
							break;
						case AppConstants.JC_JCXM_L3_1:
							(content as BzkContent).acJcxmL3b = ac;
							break;
					}
					break;
				case AppConstants.POPUP_JCNL:
					switch(_currentQueryAllView) {
						case AppConstants.JC_JCXM_0:
							(popup as JcnlDetails).acJcxm0 = ac;
							break;
						case AppConstants.JC_JCXM_1:
							(popup as JcnlDetails).acJcxm1 = ac;
							break;
						case AppConstants.VIEW_JCCS:
							(popup as JcnlDetails).acJccs = ac;
							break;
					}
					break;
				case AppConstants.VIEW_JSDYQX:
					switch(_currentQueryAllView) {
						case AppConstants.SYSAUTH_SYSFORMS:
							(content as JsDyQxContent).formsAll = ac;
							break;
						case AppConstants.SYSAUTH_SYSROLES:
							(content as JsDyQxContent).roles = ac;
							break;
					}
					break;
				case AppConstants.POPUP_EMPLOYEE_ROLE:
					switch(_currentQueryAllView) {
						case AppConstants.SYSAUTH_SYSROLES:
							(popup as EmployeeRoleDetails).roles = ac;
							break;
					}
					break;
				case AppConstants.VIEW_GCXX:
					switch(_currentQueryAllView) {
						case AppConstants.VIEW_ZJZ:
							(content as GcxxContent).cbGcxx.acJdz = ac;
							break;
					}
					break;
				case AppConstants.VIEW_JCBG:
					switch(_currentQueryAllView) {
						case AppConstants.VIEW_ZJZ:
							(content as JcbgContent).cbJcbg.acJdz = ac;
							break;
						case AppConstants.JCJG_GC_RPT_XMDM:
							(content as JcbgContent).cbJcbg.acXmdm = ac;
							break;
					}
					break;
				case AppConstants.VIEW_CJSJ:
					switch(_currentQueryAllView) {
						case AppConstants.VIEW_ZJZ:
							(content as CjsjContent).cbCjsj.acJdz = ac;
							break;
						case AppConstants.JCJG_GC_RPT_XMDM:
							(content as CjsjContent).cbCjsj.acXmdm = ac;
							break;
					}
					break;
				case AppConstants.VIEW_XGJL:
					switch(_currentQueryAllView) {
						case AppConstants.VIEW_ZJZ:
							(content as XgjlContent).cbXgjl.acJdz = ac;
							break;
						case AppConstants.JCJG_GC_RPT_XMDM:
							(content as XgjlContent).cbXgjl.acXmdm = ac;
							break;
					}
					break;
				case AppConstants.VIEW_JZJL:
					switch(_currentQueryAllView) {
						case AppConstants.VIEW_ZJZ:
							(content as JzjlContent).cbJzjl.acJdz = ac;
							break;
						case AppConstants.VIEW_JCJG:
							(content as JzjlContent).cbJzjl.acJcjg = ac;
							break;
					}
					break;
				case AppConstants.VIEW_JCHT:
					switch(_currentQueryAllView) {
						case AppConstants.VIEW_ZJZ:
							(content as JchtContent).cbJcht.acJdz = ac;
							break;
						case AppConstants.VIEW_JCJG:
							(content as JchtContent).cbJcht.acJcjg = ac;
							break;
					}
					break;
				case AppConstants.VIEW_JCFA:
					switch(_currentQueryAllView) {
						case AppConstants.VIEW_ZJZ:
							(content as JcfaContent).cbJcfa.acJdz = ac;
							break;
						case AppConstants.VIEW_JCJG:
							(content as JcfaContent).cbJcfa.acJcjg = ac;
							break;
						case AppConstants.POPUP_JCFA:
							(popup as JcfaDetails).ry = ac;
							break;
					}
					break;
				case AppConstants.VIEW_CXTJ:
					switch(_currentQueryAllView) {
						case AppConstants.VIEW_ZJZ:
							(content as CxtjContent).cbCxtj.acJdz = ac;
							break;
						case AppConstants.VIEW_JCJG:
							(content as CxtjContent).cbCxtj.acJcjg = ac;
							break;
					}
					break;
			}
		}
		
		// query bz for xm
		private function queryBzForXm(e:TreeItemEvent):void {
			_currentView = AppConstants.VIEW_JCXMDYBZ;
			if(!altQuery) altQuery = new kbmMWQuery();
			altQuery.addEventListener(kbmMWEvent.QueryDataAvailableEvent, queryBzForXm_ResultHandler);
			altQuery.addEventListener(kbmMWEvent.FailureEvent, queryBzForXm_FailureHandler);
			altQuery.Connection = _conn;
			var s:String = 'SELECT * FROM jc_jcxm_bz INNER JOIN jc_jcbz ON jc_jcxm_bz.BZID = jc_jcbz.BZID WHERE jc_jcxm_bz.JCXMID = '+ e.node.@JCXMID;
			altQuery.Query = s;
			altQuery.Open();
		}
		
		private function queryBzForXm_ResultHandler(event:Event):void {
			(event.target as kbmMWQuery).removeEventListener(kbmMWEvent.QueryDataAvailableEvent, queryBzForXm_ResultHandler);
			(event.target as kbmMWQuery).removeEventListener(kbmMWEvent.FailureEvent, queryBzForXm_FailureHandler);
			
			(content as JcxmDyBzContent).dgBz.dataProvider = new ArrayCollection((event.target as kbmMWQuery).Rows.slice());
		}
		
		private function queryBzForXm_FailureHandler(event:Event):void {
			var q:kbmMWQuery = event.target as kbmMWQuery;
			q.removeEventListener(kbmMWEvent.QueryDataAvailableEvent, queryBzForXm_ResultHandler);
			q.removeEventListener(kbmMWEvent.FailureEvent, queryBzForXm_FailureHandler);
			var result:kbmMWResponse;
			result = (event as kbmMWEvent).resultObject as kbmMWResponse;
			Alert.show("Query failure:"+result.header.StatusText+"\n"); 
		}
		
		// query bz for cs
		private function queryBzForCs(e:TreeItemEvent):void {
			_currentView = AppConstants.VIEW_JCCSDYBZ;
			if(!altQuery) altQuery = new kbmMWQuery();
			altQuery.addEventListener(kbmMWEvent.QueryDataAvailableEvent, queryBzForCs_ResultHandler);
			altQuery.addEventListener(kbmMWEvent.FailureEvent, queryBzForCs_FailureHandler);
			altQuery.Connection = _conn;
			var s:String = 'SELECT * FROM jc_jccs_bz INNER JOIN jc_jcbz ON jc_jccs_bz.BZID = jc_jcbz.BZID WHERE jc_jccs_bz.JCCSID = '+ e.node.@CSID;
			altQuery.Query = s;
			altQuery.Open();
		}
		
		private function queryBzForCs_ResultHandler(event:Event):void {
			(event.target as kbmMWQuery).removeEventListener(kbmMWEvent.QueryDataAvailableEvent, queryBzForCs_ResultHandler);
			(event.target as kbmMWQuery).removeEventListener(kbmMWEvent.FailureEvent, queryBzForCs_FailureHandler);
			
			(content as JccsDyBzContent).dgBz.dataProvider = new ArrayCollection((event.target as kbmMWQuery).Rows.slice());
		}
		
		private function queryBzForCs_FailureHandler(event:Event):void {
			var q:kbmMWQuery = event.target as kbmMWQuery;
			q.removeEventListener(kbmMWEvent.QueryDataAvailableEvent, queryBzForCs_ResultHandler);
			q.removeEventListener(kbmMWEvent.FailureEvent, queryBzForCs_FailureHandler);
			var result:kbmMWResponse;
			result = (event as kbmMWEvent).resultObject as kbmMWResponse;
			Alert.show("Query failure:"+result.header.StatusText+"\n"); 
		}
		
		// query jcjg for jdz
		private function queryJcjgForJdz(e:QueryEvent):void {
			searchAll(e.table, e.conditions, e.view, queryJcjgForJdz_resultHandler);
		}
		
		private function queryJcjgForJdz_resultHandler(value:ArrayCollection):void {
			switch(_currentView) {
				/*case AppConstants.VIEW_JCJG:
					(content as UserJcjgContent).cbUserJcjg.acJcjg = value;
					break;*/
				case AppConstants.VIEW_JCRY:
					(content as JcryContent).cbJcry.acJcjg = value;
					(content as JcryContent).cbJcry.conditions = whereConditions;
					break;
				case AppConstants.VIEW_GCXX:
					(content as GcxxContent).cbGcxx.acJcjg = value;
					break;
				case AppConstants.VIEW_JCBG:
					(content as JcbgContent).cbJcbg.acJcjg = value;
					break;
				case AppConstants.VIEW_CJSJ:
					(content as CjsjContent).cbCjsj.acJcjg = value;
					(content as CjsjContent).cbCjsj.conditions = whereConditions;
					break;
				case AppConstants.VIEW_XGJL:
					(content as XgjlContent).cbXgjl.acJcjg = value;
					break;
				case AppConstants.VIEW_JZJL:
					(content as JzjlContent).cbJzjl.acJcjg = value;
					break;
				case AppConstants.VIEW_JCHT:
					(content as JchtContent).cbJcht.acJcjg = value;
					break;
				case AppConstants.VIEW_JCFA:
					(content as JcfaContent).cbJcfa.acJcjg = value;
					break;
				case AppConstants.VIEW_CXTJ:
					(content as CxtjContent).cbCxtj.acJcjg = value;
					break;
			}
		}
		
		// jcxm l3
		// query jcxm l3 for category
		private function queryJcxmL3ForCategory(e:QueryEvent):void {
			searchAll(e.table, e.conditions, e.view, queryJcxmL3ForCategory_resultHandler);
		}
		
		private function queryJcxmL3ForCategory_resultHandler(value:ArrayCollection):void {
			switch(_currentView) {
				case AppConstants.VIEW_JCXMDYBZ:
					(content as JcxmDyBzContent).ddl1.dataProvider = value;
					(content as JcxmDyBzContent).ddl1.selectedIndex = 0;
					break;
				case AppConstants.VIEW_JCCSDYBZ:
					(content as JccsDyBzContent).ddl1.dataProvider = value;
					(content as JccsDyBzContent).ddl1.selectedIndex = 0;
					break;
			}
			
		}
		
		// search bz for jcxm l3
		private function searchBzForJcxmL3(e:QueryEvent):void {
			searchAll(e.table, e.conditions, e.view, searchBzForJcxmL3_resultHandler);
		}
		
		private function searchBzForJcxmL3_resultHandler(value:ArrayCollection):void {
			switch(_currentView) {
				case AppConstants.VIEW_JCXMDYBZ:
					(content as JcxmDyBzContent).dgBzAll.dataProvider = value;
					break;
				case AppConstants.VIEW_JCCSDYBZ:
					(content as JccsDyBzContent).dgBzAll.dataProvider = value;
					break;
			}
		}
		
		// query cs for xm
		private function queryCsForXm(e:TreeItemEvent):void {
			_currentView = AppConstants.VIEW_JCCS;
			_currentTable = AppConstants.JC_JCCS;
			if(!query) query = new kbmMWQuery();
			whereConditions = 'CSDM LIKE \''+ e.node.@XMDM + '%\'';
			queryCount();
		}
		
		// query bz for jcxm l3
		private function queryBzForJcxmL3(e:TreeItemEvent):void {
			_currentView = AppConstants.VIEW_BZK;
			_currentTable = AppConstants.JC_JCBZ;
			if(!query) query = new kbmMWQuery();
			whereConditions = 'XMDM = '+ e.node.@XMDM;
			queryCount();
		}
		
		// searh bz for jcxm l3 in bzk 
		private function searchBzForJcxmL3_Bzk(e:QueryEvent):void {
			_currentView = AppConstants.VIEW_BZK;
			_currentTable = AppConstants.JC_JCBZ;
			if(!query) query = new kbmMWQuery();
			whereConditions = e.conditions;
			queryCount();
		}
		
		// edit role for employee
		private function queryRoleForEmployee(e:QueryEvent):void {
			_currentView = e.view;
			whereConditions = e.conditions;
			if(!altQuery) altQuery = new kbmMWQuery();
			altQuery.addEventListener(kbmMWEvent.QueryDataAvailableEvent, queryRoleForEmployee_ResultHandler);
			altQuery.addEventListener(kbmMWEvent.FailureEvent, queryRoleForEmployee_FailureHandler);
			altQuery.Connection = _conn;
			var s:String = 'SELECT * FROM sysauth_employeeroles WHERE '+ e.conditions;
			altQuery.Query = s;
			altQuery.Open();
		}
		
		private function queryRoleForEmployee_ResultHandler(event:Event):void {
			(event.target as kbmMWQuery).removeEventListener(kbmMWEvent.QueryDataAvailableEvent, queryRoleForEmployee_ResultHandler);
			(event.target as kbmMWQuery).removeEventListener(kbmMWEvent.FailureEvent, queryRoleForEmployee_FailureHandler);
			
			var row:kbmMWRow = (event.target as kbmMWQuery).Rows.slice()[0];
			if(row) {
				popup.roleid_old = row['roleid'];
			} else {
				popup.roleid_old = 0;
			}
			popup.addEventListener(CloseEvent.CLOSE, closeTW);
			popup.addEventListener(EmployeeRoleDetails.UPDATE_EMPLOYEE_ROLE, updateEmployeeRole);
			
			PopUpManager.addPopUp(popup, FlexGlobals.topLevelApplication as DisplayObject, true);
			PopUpManager.centerPopUp(popup);
			
			queryAll([{table: AppConstants.SYSAUTH_SYSROLES, view: AppConstants.SYSAUTH_SYSROLES}]);
		}
		
		private function queryRoleForEmployee_FailureHandler(event:Event):void {
			var q:kbmMWQuery = event.target as kbmMWQuery;
			q.removeEventListener(kbmMWEvent.QueryDataAvailableEvent, queryRoleForEmployee_ResultHandler);
			q.removeEventListener(kbmMWEvent.FailureEvent, queryRoleForEmployee_FailureHandler);
			var result:kbmMWResponse;
			result = (event as kbmMWEvent).resultObject as kbmMWResponse;
			Alert.show("Query failure:"+result.header.StatusText+"\n"); 
		}
		
		// update role for employee
		private function updateEmployeeRole(e:Event):void {
			var tw:EmployeeRoleDetails = e.target as EmployeeRoleDetails;
			var sql:String;
			if(tw.roleid_old) {
				sql = 'UPDATE '+AppConstants.SYSAUTH_EMPLOYEEROLES +' SET roleid = \''+tw.roleid_new+'\' WHERE ' + whereConditions;
			} else  {
				var arr:Array = whereConditions.split(' = ');
				sql = 'INSERT INTO '+AppConstants.SYSAUTH_EMPLOYEEROLES+' (personid, roleid, RYBH, JSBH) VALUES ('+arr[1]+', '+tw.roleid_new+', \''+arr[1]+'\', \''+tw.roleid_new+'\')';
			}
			var q:kbmMWQuery = new kbmMWQuery();
			q.Connection = _conn;
			q.Query = sql;
			q.Execute(updateEmployeeRoleSuccess, onResolveFailed);
		}
		
		private function updateEmployeeRoleSuccess(o:Object):void {
			popup.close();
		}
		
		// show jczz for user
		private function queryJczzForUser(e:QueryEvent):void {
			_currentView = e.view;
			whereConditions = e.conditions;
			if(!altQuery) altQuery = new kbmMWQuery();
			altQuery.addEventListener(kbmMWEvent.QueryDataAvailableEvent, queryJczzForUser_ResultHandler);
			altQuery.addEventListener(kbmMWEvent.FailureEvent, queryJczzForUser_FailureHandler);
			altQuery.Connection = _conn;
			var s:String = 'SELECT * FROM jcjg_user_jcnl WHERE '+ e.conditions;
			altQuery.Query = s;
			altQuery.Open();
		}
		
		private function queryJczzForUser_ResultHandler(event:Event):void {
			(event.target as kbmMWQuery).removeEventListener(kbmMWEvent.QueryDataAvailableEvent, queryJczzForUser_ResultHandler);
			(event.target as kbmMWQuery).removeEventListener(kbmMWEvent.FailureEvent, queryJczzForUser_FailureHandler);
			
			var row:kbmMWRow = (event.target as kbmMWQuery).Rows.slice()[0];
			if(row) {
				var canshus_zz:String = row['canshus_zz'];
				var csids:Array = canshus_zz.split(',');
				popup.jcdwid = row['JCDWID'];
			} else {
				csids = [];
				popup.jcdwid = 0;
			}
			popup.csidsArray = csids;
			popup.addEventListener(CloseEvent.CLOSE, closeTW);
			popup.addEventListener(JcnlDetails.UPDATE_JCNL, updateJcnl);
			
			PopUpManager.addPopUp(popup, FlexGlobals.topLevelApplication as DisplayObject, true);
			PopUpManager.centerPopUp(popup);
			
			queryAll([{table: AppConstants.JC_JCXM, view: AppConstants.JC_JCXM_0, where: 'CategoryId = 0'}, 
				{table: AppConstants.JC_JCXM, view: AppConstants.JC_JCXM_1, where: 'CategoryId > 0'},
				{table: AppConstants.JC_JCCS, view: AppConstants.VIEW_JCCS, where: ''}]);
		}
		
		private function queryJczzForUser_FailureHandler(event:Event):void {
			var q:kbmMWQuery = event.target as kbmMWQuery;
			q.removeEventListener(kbmMWEvent.QueryDataAvailableEvent, queryJczzForUser_ResultHandler);
			q.removeEventListener(kbmMWEvent.FailureEvent, queryJczzForUser_FailureHandler);
			var result:kbmMWResponse;
			result = (event as kbmMWEvent).resultObject as kbmMWResponse;
			Alert.show("Query failure:"+result.header.StatusText+"\n"); 
		}
		
		// update jcnl
		private function updateJcnl(e:Event):void {
			var tw:JcnlDetails = e.target as JcnlDetails;
			var sql:String;
			if(tw.jcdwid) {
				sql = 'UPDATE '+AppConstants.JCJG_USER_JCNL+' SET canshus_zz = \''+tw.csids+'\' WHERE ' + whereConditions;
			} else  {
				var arr:Array = whereConditions.split(' = ');
				sql = 'INSERT INTO '+AppConstants.JCJG_USER_JCNL+' (JCDWID, canshus_jl, canshus_zz) VALUES ('+arr[1]+', \'\', \''+tw.csids+'\')';
			}
			var q:kbmMWQuery = new kbmMWQuery();
			q.Connection = _conn;
			q.Query = sql;
			q.Execute(updateJcnlSuccess, onResolveFailed);
		}
		
		private function updateJcnlSuccess(o:Object):void {
			popup.close();
		}
		
		// queryFormsByRole
		private function queryFormsByRole(e:QueryEvent):void {
			searchAll(e.table, e.conditions, e.view, queryFormsByRole_resultHandler);
		}
		
		private function queryFormsByRole_resultHandler(value:ArrayCollection):void {
			(content as JsDyQxContent).formsByRole = value;
		}
		
		// updateRoleForms
		private function updateRoleForms(e:Event):void {
			_currentView = AppConstants.VIEW_JSDYQX;
			_array_updateRoleForms = (content as JsDyQxContent).actionArray;
			_roleid_updateRoleForms = (content as JsDyQxContent).ddlRoles.selectedItem['roleid'];
			if(_array_updateRoleForms.length) {
				_index_updateRoleForms = 0;
				updateNextRoleForm();
			}
		}
		
		private function updateNextRoleForm():void {
			switch(_array_updateRoleForms[_index_updateRoleForms]['action']) {
				case 'insert':
					var a:String = Math.random().toString();
					var b:String = Math.random().toString();
					sql = 'INSERT INTO sysauth_sysroleforms (roleid, formid, JSBH, GNBH) VALUES ('+_roleid_updateRoleForms+', '+_array_updateRoleForms[_index_updateRoleForms]['formid']+', \''+ a +'\',\''+b+'\')';
					break;
				case 'delete':
					sql = 'DELETE FROM sysauth_sysroleforms WHERE roleid = ' + _roleid_updateRoleForms + ' AND formid = ' + _array_updateRoleForms[_index_updateRoleForms]['formid'];
					break;
			}
			var q:kbmMWQuery = new kbmMWQuery();
			q.Connection = _conn;
			q.Query = sql;
			q.Execute(updateRoleFormsSuccess, onResolveFailed);
		}
		
		private function updateRoleFormsSuccess(o:Object):void {
			_index_updateRoleForms++;
			if(_index_updateRoleForms < _array_updateRoleForms.length) {
				updateNextRoleForm();
			} else {
				Alert.show('更新权限成功！','系统提示');
				searchAll(AppConstants.SYSAUTH_SYSROLEFORMS, 'roleid = '+_roleid_updateRoleForms, AppConstants.VIEW_JSDYQX, queryFormsByRole_resultHandler);
			}
		}
		
		// showAddRolePopup
		private function showAddRolePopup(e:Event):void {
			_currentView = AppConstants.POPUP_ROLE;
			popup.addEventListener(CloseEvent.CLOSE, closeTW);
			popup.addEventListener(CRUDEvent.INSERT, isnertRole);
			
			PopUpManager.addPopUp(popup, FlexGlobals.topLevelApplication as DisplayObject, true);
			PopUpManager.centerPopUp(popup);
		}
		
		private function isnertRole(e:CRUDEvent):void {
			_currentView = AppConstants.POPUP_ROLE;
			var a:String = Math.random().toString();
			sql = 'INSERT INTO sysauth_sysroles (JSMC, JSBH) VALUES (\''+e.data+'\', \''+a+'\')';
			
			var q:kbmMWQuery = new kbmMWQuery();
			q.Connection = _conn;
			q.Query = sql;
			q.Execute(insertRoleSuccess, onResolveFailed);
		}
		
		private function insertRoleSuccess(o:Object):void {
			popup.close();
			_currentView = AppConstants.VIEW_JSDYQX;
			queryAll([{table:AppConstants.SYSAUTH_SYSROLES, view:AppConstants.SYSAUTH_SYSROLES}]);
		}
	}
}